package net.nampham.visioncontrol.model;

public class Line2 {
	float a;
	float b;
	float c;	
	
	public Line2(Vec2 p1, Vec2 p2){
		Vec2 d = p1.dec(p2);
		a = d.y;
		b = -d.x;
		c = -(a*p1.x+b*p1.y);
	}
	
	public enum Line2Line{
		PARALLEL, IDENTICAL, INTERSECT
	};
	
	Line2Line IntersectionType(Line2 other){		
		float denom = a*other.b-other.a*b;
		float nom1 = b*other.c - other.c*b;
		float nom2 = other.a*c - a*other.c;
		if (denom == 0) {
			if (nom1==0 && nom2==0)
				return Line2Line.IDENTICAL;
			else
				return Line2Line.PARALLEL;
		}
		else 
			return Line2Line.INTERSECT;
	}
	
	public Vec2 Intersect(Line2 other){
		float denom = a*other.b-other.a*b;
		float nom1 = b*other.c - other.b*c;
		float nom2 = other.a*c - a*other.c;
		if (denom == 0) {
			if (nom1==0 && nom2==0)
				return null;//identical lines
			else
				return null;//parallel lines
		}
		else{
			return new Vec2(nom1/denom, nom2/denom);
		}		
	}
}
